package com.david.webtest

import android.graphics.Bitmap
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.gif.GifDrawable
import com.bumptech.glide.request.FutureTarget
import java.io.ByteArrayOutputStream


class GlideUtil {
    companion object{
        fun syncLoad(url: String?, type: String): ByteArray? {
            val isGif = type.endsWith("gif")
            if (isGif) {
                try {
                    val target: FutureTarget<ByteArray> = Glide.with(WebApplication.instance)
                        .`as`<ByteArray>(ByteArray::class.java)
                        .load(url)
                        .decode(GifDrawable::class.java).submit()
                    return target.get()
                } catch (e: Exception) {
                    e.printStackTrace()
                }
                return null
            }
            val target: FutureTarget<Bitmap> = Glide.with(WebApplication.instance)
                .asBitmap().load(url).submit()
            try {
                val bitmap = target.get()
                val baos = ByteArrayOutputStream()
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos)
                return baos.toByteArray()
            } catch (e: Exception) {
                e.printStackTrace()
            }
            return null
        }
    }

}